package com.sky.web.data1.user.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sky.web.data1.user.entity.User;
import com.sky.web.data1.user.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(description = "data1-用户")
@Scope("prototype")
public class UserController {
	@Resource
	private UserService userService;
	
	@ApiOperation(value = "获取用户")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id" , value = "id" , paramType = "query" , required = false ),
		@ApiImplicitParam(name = "name" , value = "name" , paramType = "query" , required = false )
	})
	@RequestMapping(value="/getUsers", method = RequestMethod.GET)
	public List<User> getUsers(User user){
		System.out.println(user.toString());
		List<User> list = userService.getUsers(user);
		return list;
	}
	
	@ApiOperation(value = "添加用户")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id" , value = "id" , paramType = "query" , required = true ),
		@ApiImplicitParam(name = "name" , value = "name" , paramType = "query" , required = true ),
		@ApiImplicitParam(name = "pwd" , value = "pwd" , paramType = "query" , required = true )
	})
	@RequestMapping(value="/addUser", method = RequestMethod.POST)
	public String addUser(User user){
		try {
			userService.addUser(user);
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
		return "success";
	}
	
	@ApiOperation(value = "删除用户")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id" , value = "id" , paramType = "query" , required = true ),
	})
	@RequestMapping(value="/delUserById", method = RequestMethod.GET)
	public String delUserById(int id){
		try {
			userService.delUserById(id);
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
		return "succes";
	}

}
